package com.mock.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.mock.dao.TrustRegionDao;
import com.mock.model.DataTable;
import com.mock.model.TrustRegion;
import com.mock.utils.HibernateUtil;

public class TrustRegionDaoImp extends GenericDaoImpl<TrustRegion, Long>
		implements TrustRegionDao {

	@Override
	public List<TrustRegion> getAllTrustRegion() {
		return super.getAll(TrustRegion.class);
	}

	@Override
	public TrustRegion getById(Long id) {
		return super.getById(TrustRegion.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TrustRegion> getTrustRegionByQuery(DataTable datatable) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(TrustRegion.class,
				"trustregion");
		criteria.createAlias("trustregion.country", "country");
		criteria.setFetchMode("trustregion.country", FetchMode.JOIN);

		// Filter by Letter
		Disjunction disjunction = Restrictions.disjunction();
		if ("^[0-9]".equalsIgnoreCase(datatable.sSearch_0)) {
			for (int i = 0; i < 10; i++) {
				disjunction.add(Restrictions.ilike("name", i + "%"));
			}
			criteria.add(disjunction);
		} else {
			criteria.add(Restrictions.ilike("name",
					datatable.sSearch_0.replace("^", "") + "%"));
		}

		// Get Sort Column Name
		String columnName = "name";
		switch (datatable.iSortCol_0) {
		case 0:
			columnName = "name";
			break;
		case 1:
			columnName = "description";
			break;
		case 2:
			columnName = "country.countryName";
			break;
		case 3:
			columnName = "active";
			break;
		default:
			break;
		}

		// Get Sort Direction
		if ("asc".equalsIgnoreCase(datatable.sSortDir_0)) {
			criteria.addOrder(Order.asc(columnName));
		} else {
			criteria.addOrder(Order.desc(columnName));
		}
		
		// Filter in-active
		if ("".equalsIgnoreCase(datatable.sSearch_3)) {

		} else if ("Yes".equalsIgnoreCase(datatable.sSearch_3)) {
			criteria.add(Restrictions.eq("active", true));
		}

		// Paging
		criteria.setFirstResult(datatable.iDisplayStart);
		criteria.setMaxResults(datatable.iDisplayLength);

		List<TrustRegion> trustRegions = criteria.list();
		session.getTransaction().commit();
		return trustRegions;
	}

	@Override
	public int getRowCount() {
		return super.getRowCount(TrustRegion.class);
	}

	@Override
	public Long getTotalRecordByQuery(DataTable datatable) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(TrustRegion.class,
				"trustregion");
		criteria.createAlias("trustregion.country", "country");
		criteria.setFetchMode("trustregion.country", FetchMode.JOIN);

		// Filter by Letter
		Disjunction disjunction = Restrictions.disjunction();
		if ("^[0-9]".equalsIgnoreCase(datatable.sSearch_0)) {
			for (int i = 0; i < 10; i++) {
				disjunction.add(Restrictions.ilike("name", i + "%"));
			}
			criteria.add(disjunction);
		} else {
			criteria.add(Restrictions.ilike("name",
					datatable.sSearch_0.replace("^", "") + "%"));
		}

		// Filter in-active
		if ("".equalsIgnoreCase(datatable.sSearch_3)) {

		} else if ("Yes".equalsIgnoreCase(datatable.sSearch_3)) {
			criteria.add(Restrictions.eq("active", true));
		}

		Long sizes = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();		
		session.getTransaction().commit();
		return sizes;
	}

}
